<?php
namespace app\api\controller\soft;
use app\providers\BaseController;
use base\service\SmsApi;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("短信相关")
 * @Apidoc\Group("base")
 * @Apidoc\Sort(1)
 */
class Sms extends BaseController
{
	
	/**
     * 发送短信验证码
     * @Apidoc\Author("weifashi")
	 * @Apidoc\Url("/api/soft/sms/sendCode")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("phone",require=true,default="17301909836",desc="手机号码" )
	 * @param [type] $phone
	 * @global login false
	 * @return void
     */
	public function sendCode($phone){
		if( !trim($phone) ){
			return  \Api::ReError("请输入手机号！");
		}
		if( preg_match("/^1[3456789]\d{9}$/", $phone) == 0 ){
			return  \Api::ReError("请输入正确的手机号码！");
		}
		try {
			//发送
			$msgapi = new SmsApi;
			$res = $msgapi->send($phone);
			if(!$res){
				return  \Api::ReError( $msgapi->errorInfo ); 
			}
			return \Api::ReSucess("已发送");
		} catch (\Exception $e) {
			return  \Api::ReError($e->getMessage());	
		}
	}
	
	
	/**
     * 效验手机code
     * @Apidoc\Author("weifashi")
	 * @Apidoc\Url("/api/soft/sms/verifiCode")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("phone",require=true,default="17301909836",desc="手机号码" )
     * @Apidoc\Param("code",require=true,default="",desc="验证码" )
     * @global login false
     */
	public function verifiCode($phone,$code=0){
		if( !trim($phone) ){
			return \Api::ReError("请输入手机号！");
		}
		if( preg_match("/^1[3456789]\d{9}$/", $phone) == 0 ){
			return \Api::ReError("请输入正确的手机号码！");
		}
		try {
			//发送
			$msgapi = new SmsApi;
			if( !$res = $msgapi->verifyCode($phone,$code) ){
				return \Api::ReError( "验证码错误！" ); 
			}
			return \Api::ReSucess( md5($code) );	
		} catch (\Exception $e) {
			return \Api::ReError($e->getMessage());	
		}
	}
	
}
